#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream> 
/*
GET / HTTP/1.1\r\n
Host: 62.234.212.118:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,

*/
const std::string HttpSep = "\r\n";
const std::string wwwroot ="./wwwroot";
const std::string homepage = "index.html";

class HttpRequest
{
public:
    HttpRequest() : _req_blank(HttpSep),_path(wwwroot)
    {
    }
    bool Getline(std::string &str, std::string &line) // 处理一行
    {
        auto pos = str.find("\r\n");
        if (pos == std::string::npos)
        {
            return false;
        }
        line = str.substr(0, pos);
        str.erase(0, pos + HttpSep.size());
        return true;
    }
    void Deserialization(std::string &request)
    {
        std::string line;
        bool ok = Getline(request, line);
        if (!ok)
            return;
        _req = line; // 拿到http请求行
        while (true)
        {
            bool ok = Getline(request, line);
            if (ok && line.empty()) // 走到"报头空行分割内容处"
            {
                _req_content = request;
                break;
            }
            else if (ok && !line.empty())
            {
                _req_header.push_back(line);//请求报头
            }
            else
            {
                break;
            }
        }
    }
    void Parsereqline()
    {
        std::stringstream ss(_req);
        ss >> _method >> _url >> _http_version; //提取字符串，以空格进行提取
        if(_url == "/")
        {
            _path += _url;
            _path += homepage;//首页
        }
        else
        {
            _path += _url;//访问dira/dirb目录下
        }
    }
    void ParseSuffix()
    {
        auto pos = _path.find(".");
        if(pos == std::string::npos)
        {
            _suffix = "index.html";
        }
        else
        {
            _suffix = _path.substr(pos);
        } 
    }
    void Parse()
    {
        //分析请求行
        Parsereqline();
        ParseSuffix();//http中文件后缀信息
    }
    std::string GetfileContenHelper(const std::string &path)
    {
        std::ifstream in(_path, std::ios::binary);//以二进制读取文件
        if(!in.is_open())
        {
            return "";
        }
        in.seekg(0,in.end);
        int filesize = in.tellg();
        in.seekg(0,in.beg);

        std::string content;
        content.resize(filesize);
        in.read((char*)content.c_str(),filesize);
        in.close();
        return content;
    }
    void Debugghttp()
    {
        std::cout << "_req " << _req << std::endl;
        for (auto &e : _req_header)
        {
            std::cout << "-----" << e << std::endl;
        }
        std::cout << "_req_blank " << _req_blank << std::endl;
        std::cout << "_req_content " << _req_content << std::endl;

        std::cout << "method " << _method << std::endl;
        std::cout << "url " << _url << std::endl;
        std::cout << "http_version " << _http_version << std::endl;

        std::cout << "suffix " << _suffix <<std::endl;

    }
    std::string GetfileContent()
    {
        return GetfileContenHelper(_path);
    }
    std::string Getsuffix()
    {
        return _suffix;
    }
    std::string Get_404()
    {
        return GetfileContenHelper("./wwwroot/404.html");
    }
    ~HttpRequest()
    {
    }

private:
    std::string _req;                     // 请求行method ,url,http_version
    std::vector<std::string> _req_header; // 请求报头
    std::string _req_blank;               // 空行分隔内容
    std::string _req_content;             // 内容，有效载荷

    //解析后
    std::string _method;
    std::string _url; 
    std::string _http_version;
    std::string _path; //./wwwroot
    std::string _suffix;//请求资源后缀
};
const std::string blanksep = " ";
const std::string Linesep = "\r\n";

class HttpRespose
{
public:
    HttpRespose():_http_version("Http/1.0"),_code(200),_status_code_desc("ok"),_resp_blank(Linesep)
    {        
    }
    void setcode(int code)
    {
        _code = code;
    }
    void set_desc(const std::string &desc)
    {
        _status_code_desc = desc;
    }
    void Makestatsline()
    {
        //"Http/1.0 200 OK\r\n"
        _status_line = _http_version + blanksep + std::to_string(_code) + _status_code_desc + Linesep;
    }
    void AddHeader(std::string &Hcontent)
    {
        //"Content-Length: " + SuffixTOtype(http.Getsuffix()) +"\r\n"  报头
        _resq_header.push_back(Hcontent);
    }
    void Addcontent(std::string &content)
    {
        _resp_content = content;
    }
    std::string Serilize()
    {
        std::string response = _status_line;
        for(auto & e : _resq_header)
        {
            response += e;
        }
        response+=_resp_blank;
        response+=_resp_content;
        return response;
    }
    ~HttpRespose()
    {}
private:
    std::string _status_line;//请求行
    std::vector<std::string> _resq_header;//回应的报头
    std::string _resp_blank;//空行
    std::string _resp_content;//回复的网页内容

    std::string _http_version;
    int _code;
    std::string _status_code_desc;// ok
};